package com.example.contacts;

import android.R.integer;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class UpdateActivity extends Activity {

	EditText updateNameEditText;
	EditText updateNumberEditText;
	EditText updateNoteEditText;

	Button updateButton;
	Button updateCancelButton;

	String id;

	OnClickListener listener=new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.button_update:
				if(updateNameEditText.getText().toString().equals("")||updateNumberEditText.getText().toString().equals("")){

					new  AlertDialog.Builder(UpdateActivity.this).setTitle("提示" ).setMessage("姓名或者电话不能为空" ).setPositiveButton("确定" ,  null ).show();  

				}
		else{
					Intent data=new Intent();
					data.putExtra("newid", id);
					System.out.println(id);
					data.putExtra("newname", updateNameEditText.getText().toString());
					data.putExtra("newnumber", updateNumberEditText.getText().toString());
					data.putExtra("newnote", updateNoteEditText.getText().toString());
					setResult(RESULT_OK, data);
					finish();

				}
				break;
			case R.id.button_update_cancel:
				setResult(RESULT_CANCELED);
				finish();
				break;

			default:
				break;
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_update);
		init();
		Intent intent=getIntent();
		id=intent.getStringExtra("id");
		System.out.println(id);
		updateNameEditText.setText(intent.getStringExtra("oldname"));
		updateNumberEditText.setText(intent.getStringExtra("oldnumber"));
		updateNoteEditText.setText(intent.getStringExtra("oldnote"));
	}

	private void init(){
		updateNameEditText=(EditText)findViewById(R.id.editText_updatename);
		updateNumberEditText=(EditText)findViewById(R.id.editText_updatenumber);
		updateNoteEditText=(EditText)findViewById(R.id.editText_updatenote);
		updateButton=(Button)findViewById(R.id.button_update);
		updateCancelButton=(Button)findViewById(R.id.button_update_cancel);
		updateButton.setOnClickListener(listener);
		updateCancelButton.setOnClickListener(listener);
	}

}
